<template>
  <view >
    <mySearch bg-color="#ffcfe5" @click="goSearch"></mySearch>
    <view class="scroll-view-container">
      
      <scroll-view :style="{height: vw + 'px'}" scroll-y class="left-scroll-view" >
        <!-- 一级菜单 -->
        <block v-for="(item,index) in cateList" :key="index">
          <view class="left-scroll-view-item" :class="{ 'active': activeIndex===index }" @click="changeCate(index)">
            {{item.cat_name}}</view>
        </block>
      </scroll-view>
      <scroll-view class="right-scroll-view" scroll-y :style="{height: vw + 'px'}" scroll-top="scrollTop">
        <!-- 二级菜单 -->
        <view class="cate-lv2" v-for="(val,idx) in nextCateList" :key="idx">
          <view class="cate-lv2-title">/{{val.cat_name}}/</view>
          <view class="cate-lv3-list">
            <!-- 三级分类 Item 项 -->
            <view class="cate-lv3-item" v-for="(valu, idex) in val.children" :key="idex" @click="goList(valu)">
              <!-- 图片 -->
              <image :src="valu.cat_icon"></image>
              <!-- 文本 -->
              <text>{{valu.cat_name}}</text>
            </view>
          </view>
        </view>
        <!-- 三级菜单 -->
        <!--  -->
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import badgemix from '../../mixins/tab-bar.js'
  export default {
     mixins: [badgemix],
    data() {
      return {
        vw: 0,
        activeIndex: 0,
        cateList: [],
        nextCateList: [],
        scrollTop:0
      }
    },
    onLoad() {
      this.vw = uni.getSystemInfoSync().windowHeight - 50
      this.getCateList()
    },
    methods: {
      async getCateList() {
        let {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        console.log(res);
        if (res.meta.status !== 200) return uni.$showToast()
        this.cateList = res.message
        this.nextCateList = res.message[0].children
      },
      changeCate(idx) {
        this.activeIndex = idx
         this.nextCateList = this.cateList[idx].children
          // 让 scrollTop 的值在 0 与 1 之间切换
           this.scrollTop = this.scrollTop === 0 ? 1 : 0
      },
      goSearch() {
        uni.navigateTo({
          url:'../../subpkg/search/search'
        })
      },
      goList(val) {
        console.log(val);
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid='+val.cat_id
        })
      }
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;
    .left-scroll-view {
      width: 120px;

      .left-scroll-view-item {
        line-height: 60px;
        background-color: #f7f7f7;
        text-align: center;
        font-size: 12px;

        // 激活项的样式
        &.active {

          background-color: #ffffff;
          position: relative;

          // 渲染激活项左侧的红色指示边线
          &::before {
            content: ' ';
            display: block;
            width: 3px;
            height: 30px;
            background-color: #c00000;
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
          }
        }

        
      }
    }
    .right-scroll-view{
      .cate-lv2-title {
        font-size: 12px;
        font-weight: bold;
        text-align: center;
        padding: 15px 0;
      }
      .cate-lv3-list {
        display: flex;
        flex-wrap: wrap;
      
        .cate-lv3-item {
          width: 33.33%;
          margin-bottom: 10px;
          display: flex;
          flex-direction: column;
          align-items: center;
      
          image {
            width: 60px;
            height: 60px;
          }
      
          text {
            font-size: 12px;
          }
        }
      }
    }
  }
</style>
